<template>
	<view class="inquiry">
		<view class="content">
			<view class="card">
				<view class="head u-flex u-flex-between">
					<view>{{dataobj.facestatus==1?'已通过':'未通过'}}实名认证</view>
					<view>执业证书编号：{{dataobj.certificate_code}}</view>
					<!-- 131396565112 -->
				</view>
				<view class="userinfo u-flex">
					<image class="avatar" :src="$base.cdn(dataobj.img,'/static/lj/common/default-doctor.png')" mode="aspectFill"></image>
					<view class="u-flex-1">
						<view class="tit u-flex u-flex-items-start">
							<view class="u-flex-fill u-flex u-flex-items-end">
								<view class="name">{{dataobj.name}}</view>
								<view class="zc">{{dataobj.academic_title||'暂无职称'}}</view>
							</view>
							<image class="follow" :src="'/static/lj/common/'+(dataobj.is_follow == 0 ?'capsule_723':'capsule_724')+'.png'" mode="aspectFill" @click="Onaddfollow"></image>
						</view>
						<view>
							<uni-rate :size="12" activeColor="#ea9518" allow-half :readonly="true" :value="dataobj.score" />
						</view>
						<view class="con">
							擅长：{{dataobj.be_good_at_synopsis||'暂无'}}
							<text class="btn-text-detail" @click="goNext('/pagesA/info/docinfo?id=' + id,false)">详情</text>
						</view>
					</view>
				</view>
			</view>

			<view class="card" style="padding-bottom: 26rpx;">
				<view class="title-outer">
					<view class="title u-flex u-flex-between">
						<view class="u-flex-fill">问诊服务</view>
					</view>
				</view>
				<view class="doctor-service">
					<view class="signing u-flex" v-if="dataobj.is_sign">
						<view class="status">已签约</view>
						<view class="con u-flex u-flex-y-center">
							<view class="inn u-flex-1">
								<view class="tags u-flex u-flex-between">
									<view class="tag">图文问诊<text>不限</text></view>
									<view class="tag">视频问诊<text>不限</text></view>
									<view class="tag">报告解读<text>不限</text></view>
								</view>
								<view class="time">有效期：{{$u.timeFormat(dataobj.signOrder.starttime,'yyyy/mm/dd')}}-{{$u.timeFormat(dataobj.signOrder.endtime,'yyy/mm/dd')}}</view>
							</view>
						</view>
					</view>

					<view class="item u-flex u-flex-between u-flex-items-end" @click="gotoDiagReport" v-if="dataobj.consultation_report_status==1">
						<view>
							<view class="tit">
								报告解读
								<text class="price">
									{{parseFloat(dataobj.consultation_report_money||0)<=0||dataobj.is_sign?'免费':'￥'+dataobj.consultation_report_money+'元/次'}}
								</text>
							</view>
							<view class="con">和医生一对一在线图片文字沟通病情，可开药开检查</view>
						</view>
						<view class="btn-img">
							<image src="/static/lj/common/capsule_435.png" mode="aspectFill"></image>
							<view class="btit">马上问诊</view>
						</view>
					</view>
					<navigator hover-class="none" :url="'/pagesL/quick/diag?type=tw&id='+dataobj.id+'&family_id='+family_id" class="item u-flex u-flex-between u-flex-items-end" v-if="dataobj.consultation_status==1">
						<view>
							<view class="tit">
								图文问诊
								<text class="price">
									{{parseFloat(dataobj.consultation_money||0)<=0||dataobj.is_sign?'免费':'￥'+dataobj.consultation_money+'元/次'}}
								</text>
							</view>
							<view class="con">使用文字、图片与名医线上交流，可开药开检查</view>
						</view>
						<view class="btn-img">
							<image src="/static/lj/common/capsule_317_2.png" mode="aspectFill"></image>
							<view class="btit">马上问诊</view>
						</view>
					</navigator>
					<navigator hover-class="none" :url="'/pagesL/quick/diag?type=sp&id='+dataobj.id+'&family_id='+family_id" class="item u-flex u-flex-between u-flex-items-end" v-if="dataobj.consultation_viedo_status==1">
						<view>
							<view class="tit">
								视频问诊
								<text class="price">
									{{parseFloat(dataobj.consultation_viedo_money||0)<=0||dataobj.is_sign?'免费':'￥'+dataobj.consultation_viedo_money+'元/次'}}
								</text>
							</view>
							<view class="con">和医生一对一在线视频沟通病情，可开药开检查</view>
						</view>
						<view class="btn-img">
							<image src="/static/lj/common/capsule_368.png" mode="aspectFill"></image>
							<view class="btit">马上问诊</view>
						</view>
					</navigator>
				</view>
				<block v-if="dataobj.is_sign==0 && signPackage.length>0">
					<view class="title-outer">
						<view class="title u-flex">
							<view>签约服务</view>
							<view class="tab-tip">开通享不限次免费问诊</view>
						</view>
					</view>
					<view class="sign-service">
						<view class="outer u-flex u-flex-wrap">
							<view class="item" v-for="(item,index) in signPackage" :key="index" @click="changeSignPackage(index)">
								<view class="inner" :class="{on:index==signPackageIndex}">
									<view class="tit">{{item.title}}</view>
									<view class="price">{{item.money}}元</view>
									<view class="con">{{parseFloat((parseFloat(item.money)/parseInt(item.num)).toFixed(2))}}元/{{item.type=='month'?'月':(item.type=='week'?'周':'天')}}</view>
								</view>
							</view>
						</view>

						<view class="btn-fillet" @click="OnSign">
							<text class="price">￥{{parseFloat(signPackage[signPackageIndex].money)}}</text>开通服务
						</view>
					</view>
				</block>
			</view>

			<view class="card">
				<view class="title-outer">
					<view class="title u-flex u-flex-between">
						<view class="u-flex-fill">用户评价</view>
						<view class="ricon u-flex" @click="goNext('/pagesA/ask/comment?id=' + id,false)">
							<view>更多</view>
							<u-icon name="arrow-right" color="#888888"></u-icon>
						</view>
					</view>
				</view>
				<view class="comment-tabs u-flex">
					<view class="item">全部（{{dataobj.common_num}}）</view>
					<view class="item">最新（{{dataobj.common_new_num}}）</view>
					<view class="item">好评（{{dataobj.common_goods_num}}）</view>
				</view>
				<view v-if="commentList.length == 0" class="loading">暂无评论</view>
				<view class="comment-list">
					<view class="item u-flex" v-for="(item,index) in commentList" :key="index">
						<image class="avatar" :src="$base.cdn(item.user_info.headimgurl)" mode="aspectFill"></image>
						<view class="u-flex-fill">
							<view class="tit u-flex">
								<view class="u-flex-fill u-flex">
									<view class="name">{{item.user_info.nickname}}</view>
									<!--<view class="zc">图文</view> -->
									<view class="zc">{{item.type=='consultation_order'?'图文问诊':item.type=='consultation_video_order'?'视频问诊':'报告解读'}}</view>
								</view>
								<uni-rate :size="12" activeColor="#ea9518" allow-half :readonly="true" :value="item.fraction" />
								<!-- <text class="font-ef9140 font14 comment_start">{{item.fraction>=4?'很好':item.fraction<=3?'一般':'好'}}</text> -->
							</view>

							<view class="con">
								<!-- 使用文字、图片与名医交流 -->
								{{item.content?item.content:'暂无'}}
							</view>
							<view class="time">
								{{item.createtime}}
								<!-- 2022.2.1 -->
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="card">
				<view class="title-outer">
					<view class="title u-flex u-flex-between">
						<view class="u-flex-fill">医生文章</view>
						<!-- <view class="ricon u-flex" @click="gotoArticleList(item)"> -->
						<view class="ricon u-flex" @click="goNext('/pagesD/doctor/articleList?id=' + id,false)">
							<view>更多</view>
							<u-icon name="arrow-right" color="#888888"></u-icon>
						</view>
					</view>
				</view>
				<view class="article-list">
					<view class="item u-flex" v-for="(item,index) in group.list" :key="index" @click="gotoArticle(item)">
						<view class="u-flex-fill">
							<view class="tit">{{item.title}}</view>
							<view class="sub-tit">{{item.doctor.name}} | {{item.doctor.practice_organization}}知名医生</view>
							<view class="u-flex u-flex-between">
								<view class="views">{{item.volume}}阅读</view>
								<view class="time">{{$u.timeFormat(item.createtime)}}</view>
							</view>
						</view>
						<image class="cover" :src="$base.cdn(item.img)" mode="aspectFill"></image>
					</view>

					<view v-if="group.loaded&&group.list.length==0" class="loading">暂无文章</view>
					<view v-else>
						<u-loadmore
							:status="loading?'loading':(group.loaded?'nomore':'loadmore')"
							fontSize="28rpx"
							color="#d9d9d9"
							nomoreText="已加载全部"
							marginTop="30rpx"
							marginBottom="0"
							@loadmore="getArticleList"
						></u-loadmore>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import ComPopuptime from '@/components/ComPopuptime.vue';
	import uniRate from '@/components/uni-rate/uni-rate.vue';
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	export default {
		components: {
			ComPopuptime,
			uniRate,
			uniPopup
		},
		data() {
			return {
				id: null,
				dataobj: {}, //医生详情
				
				family_id: 0, //就诊人id，用于复诊
				
				//切换
				navList: [{
						title: '预约挂号',
						info: '名医一键挂号',
						code: 'hang',
					},
					{
						title: '咨询问诊',
						info: '就诊更加安全',
						code: 'line',
					}
				],
				navindex: 'line', //下标
				timeList: [], //日历时间
				week: '', //周几
				day: '', //日期
				doctime: [], //医生排版时间
				docobj: {}, //选择的时间段
				familyList: [], //就诊人列表
				familyobj: {}, //默认就诊人
				commentList: [], //评论列表
				is_his: 0, //是不是又his

				// 签约套餐
				signPackage: [],
				signPackageIndex: 0,
				group:{
					page: 1,
					limit: 10,
					list: [],
					loaded: false
				},
				loading: false,
			}
		},
		onLoad: function(options) {
			this.id = options.id
			if(options.family_id) this.family_id = options.family_id
			
			//医生详情
			this.Ondoctordetails()
			//获取就诊人
			this.OnPatient()
			//医生评论
			this.Ondocevaluate()
			//公共接口
			this.Onconfig()

			// 文章列表
			this.getArticleList()
		},
		onShareAppMessage(res) {
			var data = {path: '/pagesA/ask/inquiry?id='+this.id+'&spuid='+this.$store.state.user.uid}, shareConfig = this.$store.state.shareConfig
			console.log(shareConfig)
			if(shareConfig.title) data.title = shareConfig.title
			if(shareConfig.image) data.imageUrl = this.$base.cdn(shareConfig.image)
			return data
		},
		methods: {
			Onconfig(type) {
				this.$Http({
					url: 'consultationConsult',
					data: {}
				}).then(res => {
					if (res.code == 1) {
						this.is_his = res.data.is_his
						if (res.data.is_his == 0) {
							//日期列表
							this.Ontimelist()
						}
					}
				})
			},
			//详情
			Ondoctordetails() {
				this.$Http({
					url: 'doctorDetails',
					data: {
						doctor_id: this.id,
					}
				}).then(res => {
					this.dataobj = res.data
					this.signPackage = res.data.signPackage
					if(this.dataobj.be_good_at_synopsis.length>30){
						this.dataobj.be_good_at_synopsis = this.dataobj.be_good_at_synopsis.substr(0, 30)+'......'
					}
				})
			},
			//医生评论
			Ondocevaluate() {
				this.$Http({
					url: 'listComment',
					data: {
						page: 1,
						limit: 5,
						doctor_id: this.id,
					}
				}).then(res => {
					res.data.data.forEach(item => {
						item.createtime = this.$base.formatTime(new Date(item.createtime *
							1000), "yyyy/MM/dd")
					})
					this.commentList = res.data.data
				})
			},

			getArticleList(){
				var that = this,group = that.group;
				if(that.loading || group.loaded) return;

				that.loading = true

				this.$Http({
					url: 'doctorArticleList',
					data: {
						id: this.id,
						page: group.page,
						limit: group.limit,
					},
					type:'POST',
				}).then(res=>{
					if(res.code == 1){
						var list = res.data;
						group.loaded = list.length < group.limit;
						group.page ++;
						group.list = group.list.concat(list);
					}

					that.loading = false
				}).catch(()=>{
					that.loading = false
				});
			},
			onReachBottom(){
				this.getArticleList()
			},
			gotoDiagReport(){
				this.$store.commit('selectDoctorobj', this.dataobj)
				uni.navigateTo({
					url: '/pagesL/quick/diagReport2?family_id='+this.family_id
				})
			},
			gotoArticleList(){
				uni.navigateTo({
					url: '/pagesD/doctor/articleList?id='+item.id
				})
			},
			gotoArticle(item){
				uni.navigateTo({
					url: '/pagesD/doctor/article?id='+item.id
				})
			},
			// 切换签约套餐
			changeSignPackage(index){
				this.signPackageIndex = index
			},
			//提交
			OnSign() {
				if(this.signPackageIndex<0 || this.signPackageIndex>=this.signPackage.length){
					uni.showToast({
						title: '请选择签约服务',
						icon: 'none',
						duration: 2000
					})
					return
				}

				var sign_package_uniqid = this.signPackage[this.signPackageIndex].uniqid

				let data = {
					doctor_id: this.id,
					sign_package_uniqid: sign_package_uniqid?sign_package_uniqid:0,
				}

				this.$Http({
					url: 'createConsultationSignOrder',
					data: data,
					show: true,
					type: 'post'
				}).then(res => {
					if (res.code == 1) {
						if(res.data.pay_code){
							uni.redirectTo({
								url: '/pagesL/my/signpage?type=1',
							})
						}else{
							this.paywait(res.data.order_code)
						}
					}
				})

			},
			paywait(order_code) {
				let that = this
				this.$Http({
					url: 'payConsultationSignOrder',
					type:'POST',
					data: {
						order_code: order_code
					},
				}).then(res => {
					if (res.code = 1) {
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.pacKage,
							signType: res.data.signType,
							paySign: res.data.paySign,
							success: function(res1) {
								uni.redirectTo({
									url: '/pagesL/my/signpage?type=1',
								})
							},
							fail: function(err) {
								uni.redirectTo({
									url: '/pagesL/my/signpage?type=1',
								})
							}
						});
					}
				})
			},




			//添加关注
			Onaddfollow() {
				let url = ''
				if (this.dataobj.is_follow == 0) {
					url = 'addUserDoctor'
				} else {
					url = 'deladdUserDoctor'
				}
				this.$Http({
					url: url,
					data: {
						doctor_id: this.id,
					},
					show: true,
					type: 'POST'
				}).then(res => {
					if (res.code == 1) {
						if (this.dataobj.is_follow == 1) {
							this.dataobj.is_follow = 0
						} else {
							this.dataobj.is_follow = 1
						}
					}
				})
			},
			//切换
			Onnav(e) {
				if (e.code == 'hang') {
					if (this.is_his == 1) {
						if (this.dataobj.his_id) {
							uni.navigateTo({
								url: '/pagesA/his/docyy/docyy?id=' + this.dataobj.his_id,
							});
						} else {
							uni.showToast({
								icon: 'none',
								duration: 3000,
								title: '此医师暂无挂号余号'
							});
						}
					}else{
						this.navindex = e.code
					}
				}else{
					this.navindex = e.code
				}
			},
			// 日期列表
			Ontimelist() {
				this.$Http({
					url: 'doctorDate',
					data: {
						doctor_id: this.id,
					}
				}).then(res => {
					this.timeList = this.$base.OnDate(res.data)
					this.week = res.data[0].getDay
					this.day = res.data[0].date
					this.OnDetails()
				})
			},
			//获取就诊人
			OnPatient() {
				return false;
				this.$Http({
					url: 'listThePatient',
					data: {}
				}).then(res => {
					if (res.code == 1) {
						if (!res.data || res.data.length == 0) {} else {
							this.familyList = res.data
							this.familyobj = res.data[0] ? res.data[0] : {}
							res.data.forEach(item => {
								if(item.the_patient_info) item.the_patient_info.namea = item.the_patient_info.name.substring(item.the_patient_info.name.length - 2)
								if (item.isdefault == 1) {
									this.familyobj = item
								}
							})
						}
					}
				})
			},
			//选择就诊人
			Onchooisfamily(e) {
				this.familyobj = e
			},
			//添加就诊人
			Onjzrgladd() {
				this.$store.commit('Onjzkurl', '/pagesA/ask/inquiry?id=' + this.id)
				uni.navigateTo({
					url: '/pagesA/my/jzrgladd',
				})
			},
			//选择时间
			Ontime(e) {
				this.day = e.date
				this.week = e.getDay
				this.OnDetails()
			},
			//组件确认选择时间
			OnComconfirm(e) {
				this.timeList.forEach(item => {
					if (item.day == e) {
						this.week = item.getDay
					}
				})
				this.day = e
				this.OnDetails()
			},
			//医生详情时间列表挂号周期
			OnDetails() {
				this.$Http({
					url: 'Details',
					token: false,
					data: {
						doctor_id: this.id,
						time: this.day
					}
				}).then(res => {
					if (res.code == 1) {
						let list = [{
								title: '上午',
								id: 'morning',
								begin: res.data.morning_begin_time,
								end: res.data.morning_end_time,
								moeny: res.data.money,
								num: res.data.morning_number,
							},
							{
								title: '下午',
								id: 'afternoon',
								begin: res.data.afternoon_begin_time,
								end: res.data.afternoon_end_time,
								moeny: res.data.money,
								num: res.data.afternoon_number,
							},
							{
								title: '晚上',
								id: 'night',
								begin: res.data.night_begin_time,
								end: res.data.night_end_time,
								moeny: res.data.money,
								num: res.data.night_number,
							}
						]
						this.doctime = list
					}
				})
			},
			//确认挂号
			Onconfirm() {
				let data = {
					point_type: this.docobj.id,
					doctor_id: this.dataobj.id,
					time: this.day,
					the_patient_id: this.familyobj.the_patient_info.id
				}
				this.$Http({
					url: 'createHangNumberOrder',
					data: data,
					type: 'POST',
					show: true
				}).then(res => {
					if (res.code = 1) {
						this.$refs.popup.close()
						uni.navigateTo({
							url: '/pagesA/register/conpay?id=' + res.data.order_code,
						});
					}
				})
			},
			//预约弹窗
			OnPopupshow(e) {
				console.log(e, '??')
				if (e.num < 0 || e.num == null) {
					return
				}
				this.docobj = e
				this.$refs.popup.open()
			},
			//前往问诊页面
			Ondiag(e) {
				if (e == 'tw') {
					if (this.dataobj.consultation_status != 1) {
						uni.showToast({
							icon: 'none',
							duration: 3000,
							title: '该医生暂未开始该服务,请选择其他服务'
						});
						return
					}
				}
				if (e == 'sp') {
					if (this.dataobj.consultation_viedo_status != 1) {
						uni.showToast({
							icon: 'none',
							duration: 3000,
							title: '该医生暂未开始该服务,请选择其他服务'
						});
						return
					}
				}
				uni.navigateTo({
					url: '/pagesA/ask/diag?type=' + e + '&id=' + this.id,
				})
			},
			//查看时间
			Onchoice() {
				this.$refs.compopuptime.Onshow(this.timeList, this.day);
			},
			// 路由跳转: tag默认false,tag=true跳到tabBar页面
			goNext(url, tag) {
				if (tag) {
					uni.switchTab({
						url: url
					});
				} else {
					uni.navigateTo({
						url: url
					});
				}
			},
		}
	}
</script>

<style lang="less">
	@import url("style.less");

	page{
		background-color: #f6f6f6;
	}
</style>
